package com.jm.weibo.ui;

import java.util.HashMap;
import java.util.List;

import weibo4andriod.Status;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jm.weibo.logic.IWeiboActivity;
import com.jm.weibo.logic.MainService;
import com.jm.weibo.logic.StatusAdapter;
import com.jm.weibo.logic.Task;
import com.jm.weibo.util.MyToastUtil;

 
public class MyListActivity extends Activity implements IWeiboActivity{
   
	public static final String REF_WEIBO="REF_WEIBO";//更新微博 
    
    public static final String REF_ICON="REF_ICON";//更新头像
    
    public static final String REF_WEIBO_MORE="REF_WEIBO_MORE";//更多信息
    
    public static final String REF_NEW_WEIBO_RESULT="REF_NEW_WEIBO_RESULT";//发表微博的结果
   
    public static final String REF_NEW_WEIBO_COMM_RESULT="REF_NEW_WEIBO_RESULT";//发表微博的结果
   
    //加载微博中的图片
    public static final String REF_GET_WEIBO_IMAGE = "REF_GET_WEIBO_IMAGE";
    
    ListView lv;
    View process;
    public int pagesize=5;//每页条数
    public int nowpage=1;//页数
    protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		//请求当前用户的微博主页 
		HashMap hm=new HashMap();
		hm.put("pagesize", pagesize);
		hm.put("nowpage", nowpage); //请求当前用户的微博主页 
		Task tsHome=new Task(Task.TASK_GET_USER_HOMETIMEINLINE,hm);
	    MainService.addNewTask(tsHome);
	    
	    
	    
	    MainService.allActivity.add(this);
	    //测试代码
	     //lv=new ListView(this);
	    this.setContentView(R.layout.home);
	    //获取标题
	    View titleview=this.findViewById(R.id.freelook_title);
	    
	    TextView tv=(TextView)titleview.findViewById(R.id.textView);
	    //当前用户的昵称
	    tv.setText(MainService.currentUser.getName());
	    
	    tv.setTextSize((float) 16.0);
	    
	    Button btNewbolg=(Button)titleview.findViewById(R.id.title_bt_left);
	    btNewbolg.setBackgroundResource(R.drawable.title_new);
	    btNewbolg.setOnClickListener(new OnClickListener(){
 	        
			public void onClick(View v) {
		    //进入发表微博窗口
 	    	   Intent itnewblog=new Intent(MyListActivity.this,NewWeiboActivity.class);
 	    	   MyListActivity.this.startActivityForResult(itnewblog, 1);
 	       }
	    }
	    );
	    
	    Button btRefbolg=(Button)titleview.findViewById(R.id.title_bt_right);
	    btRefbolg.setBackgroundResource(R.drawable.title_reload);
	    
	    btRefbolg.setOnClickListener(new OnClickListener()
	    {     
			 public void onClick(View arg0) {
		     		//添加获取首页信息任务
	    	HashMap hm=new HashMap();
			hm.put("pagesize", pagesize);
			hm.put("nowpage", nowpage);
	    	Task tsHome=new Task(Task.TASK_GET_USER_HOMETIMEINLINE,hm);
	    	MainService.addNewTask(tsHome);
		    
			}
	    }
	    );
	    //获取列表对象和进度条对象
	    process=this.findViewById(R.id.progress);
	    lv=(ListView)this.findViewById(R.id.freelook_listview);
	    lv.setClickable(true);
	    
	    
	    //对ListView进行事件处理
	    lv.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				//Toast.makeText(MyListActivity.this, "您选中了"+arg2, 1000).show();
				MyToastUtil.myToastShow(MyListActivity.this, "查看");
				if(arg3==-1){  //更多
					nowpage++;
					HashMap hm=new HashMap();
					hm.put("pagesize", pagesize);
					hm.put("nowpage", nowpage);
					Task tsHome=new Task(Task.TASK_GET_USER_HOMETIMEINLINE_MORE,hm);
					MainService.addNewTask(tsHome);
				}
				else if(arg3==0)
				{
					HashMap hm=new HashMap();
					hm.put("pagesize", pagesize);
					hm.put("nowpage", nowpage);
			    	Task tsHome=new Task(Task.TASK_GET_USER_HOMETIMEINLINE,hm);
			    	MainService.addNewTask(tsHome);
				}else{
				    Intent it=new Intent(MyListActivity.this,ViewWeiboActivity.class);
				   try{
					    StatusAdapter ma=(StatusAdapter)lv.getAdapter();
					    Status statu=(Status)ma.allstatus.get(arg2-1);
					    it.putExtra("status",statu);
				    }catch(Exception e){
				    	Log.e("e","error"+e);
				    }
//				    
				    MyListActivity.this.startActivityForResult(it, 0);
				}
				
			}
	    	
	    });
	    //注册上下文菜单
        this.registerForContextMenu(lv);
	}
    
     
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo am=(AdapterContextMenuInfo)item.getMenuInfo();
		
		switch(item.getItemId())
	    {case 1://
	    	Toast.makeText(this, "您要转发"+am.id,1000).show();
	    	break;
	    case 2://
	    	Toast.makeText(this, "您要评论列表第"+am.position+"项",1000).show();
	    	break;
	    case 3://
	    	Toast.makeText(this, "您要收藏",1000).show();
	    	break;
	    case 4://
	    	Toast.makeText(this, "您要查看信息",1000).show();
	    	break;
	    }
		return super.onContextItemSelected(item);
	
	
	}
	 
	public void onContextMenuClosed(Menu menu) {
   }
	
	//初始化上下文菜单
	 
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo am=(AdapterContextMenuInfo)menuInfo;
		
		if(am.position==0)//刷新
		{
			return;
		}
		if(am.id==-1)//更多
		{
			return;
		}
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("微博功能操作");
		//      组编号 项编号 排列顺序
		menu.add(1, 1, 1, "转发");
		menu.add(1, 2, 2, "评论");
		menu.add(1, 3, 3, "收藏");
		menu.add(1, 4, 4, "查看用户信息");
		
	}
	 
	public void init() {
		// TODO Auto-generated method stub
		
	}
	 
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Log.d("keydown", ".................."+keyCode);
		if(keyCode==4)//如果用户按下了返回键
		{
			MainService.promptExitApp(this);
		}
		return true;
	}
	 
	public void refresh(Object... param) {
		// TODO Auto-generated method stub
		Log.d("ref", (String)param[0]);
		String flag = (String)param[0];
		if(flag.equals("REF_WEIBO"))
	    {
			List<Status> alls=(List<Status>)param[1];
			StatusAdapter ma=new StatusAdapter(this,alls);
			lv.setAdapter(ma);//更新列表
			process.setVisibility(View.GONE);
		}
		else if(flag.equals(REF_ICON)){ 
			//让数据适配器getView方法重新调用
			((StatusAdapter)lv.getAdapter()).notifyDataSetChanged();
		}
		
		else if(flag.equals(REF_WEIBO_MORE)){
			List<Status> alls=(List<Status>)param[1];
			((StatusAdapter)lv.getAdapter()).addMoreData(alls);
		}
		
		else if(flag.equals(REF_NEW_WEIBO_RESULT)){
			String result=(String)param[1];
			Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
		}
		
		else if(flag.equals(REF_NEW_WEIBO_COMM_RESULT)){   
			String result=(String)param[1];
		    Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
	    }
	    
	    //刷新微博中的图片
		else if(flag.equals(REF_GET_WEIBO_IMAGE)){
			//让数据适配器getView方法重新调用
			((StatusAdapter)lv.getAdapter()).notifyDataSetChanged();
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode){
	      case RESULT_OK: /* 取得数据，并显示于画面上 */
	         Bundle bunde = data.getExtras();
	         String newBolgResultMsg = bunde.getString("newBlogResult");
	         Toast.makeText(this, newBolgResultMsg, Toast.LENGTH_SHORT).show();
	         break;
	         
	      default:
	        break;
	    }
	}

}
